from typing import Optional

from pydantic import BaseModel


class NetworkStatus(BaseModel):
    uplink_bandwidth_usage: int
    downlink_bandwidth_usage: int
    received_data_bytes_total: int
    sent_data_bytes_total: int

    @classmethod
    def from_data(cls, external_data):
        return cls(
            uplink_bandwidth_usage=int(external_data.get('upspeed')),
            downlink_bandwidth_usage=int(external_data.get('downspeed')),
            received_data_bytes_total=int(external_data.get('recvbytes')),
            sent_data_bytes_total=int(external_data.get('sentbytes')),
        )


class MetricsData(BaseModel):
    number: str
    phone_number: str
    business_type: str
    group: str
    remark: str
    status: str
    model: str
    carriers: str
    province: str
    platform: Optional[str] = 'onethingcloud'
    user_id: str
    broadband_number: Optional[str] = ''
    vlan_id: str
    network_type: str
    network_interface: str

    @classmethod
    def from_data(cls, external_data):
        return cls(
            number=external_data.get('number'),
            phone_number=external_data.get('phone_number'),
            business_type=external_data.get('business_type'),
            group=external_data.get('group'),
            remark=external_data.get('remark'),
            status=external_data.get('status'),
            model=external_data.get('model'),
            carriers=external_data.get('carriers'),
            province=external_data.get('province'),
            platform=external_data.get('platform'),
            user_id=external_data.get('user_id'),
            broadband_number=external_data.get("username", "null"),
            vlan_id=str(external_data.get('vlanid')),
            network_type=external_data.get('proto'),
            network_interface=external_data.get('nic'),
        )
